<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/5/9
 * Time: 10:53
 */
namespace app\home\controller;
use think\Db;
use think\Request;
class News extends Base {
    /**
     * 首页》我的消息
     * 如果m_one_user_id==user_id，取reply_name...，否则昵称取one_name...
     * 给了个user_id_other的字段，对方的id，获取聊天记录时，对方的就传这个
     */
    public function index(){
        $user_id = session('user_id');
        $data = Db::table('up_message')->alias('m')
            ->join('up_u_user u','m.m_one_user_id=u.user_id','LEFT')
            ->join('up_u_user u2','m.m_reply_user_id=u2.user_id','LEFT')
            ->where('m_one_user_id',$user_id)
            ->whereOr('m_reply_user_id',$user_id)
            ->order('m_time DESC')
            ->group('m_start,m_end')
            ->field('u.user_image one_image,u2.user_image reply_image,u.user_username one_name,u2.user_username reply_name,max(m_time) m_time,max(m_id) m_id,m_one_user_id,m_reply_user_id')
            ->paginate(10);
        $page = $data->render();
        $data = $data->all();
        if($data){
            foreach ($data as $k=>$v){
                $con = Db::table('up_message')->where('m_id',$v['m_id'])
                    ->field('m_one_user_id,m_reply_user_id,m_content')->find();
                $data[$k]['m_content'] = $con['m_content'];
                $data[$k]['user_id_other'] = $con['m_one_user_id'];
                if($con['m_one_user_id'] == $user_id){
                    $data[$k]['user_id_other'] = $con['m_reply_user_id'];
                }
                //---------------------------计算未读数量start-------------------------------------
                $noRead = Db::table('up_message')
                    ->where('m_reply_user_id',$user_id)
                    ->where('m_one_user_id',$data[$k]['user_id_other'])
                    ->where('m_read','notlike',"%,$user_id,%")
                    ->count();
                $data[$k]['no_read_num'] = $noRead;
                //---------------------------计算未读数量 end -------------------------------------
            }
        }
        $system = 1;//没有未读的
        $have = Db::table('up_notice')
            ->where('n_read','notlike',"%,$user_id,%")
            ->find();
        if($have){
            $system = 2;
        }
        $this->assign('data',$data);
        $this->assign('system',$system);
        $this->assign('page',$page);
        $this->assign('user_id',$user_id);
        $this->assign('title','消息');
        return $this->fetch('user/news_my');
    }
    /**
     * 系统消息列表
     */
    public function sysnews(){
        $user_id = session('user_id');
        $data = Db::table('up_notice')
            ->order('n_time DESC')
            ->paginate(10);
        $page = $data->render();
        $data = $data->all();
        //列表处就得把文字类型的先改为已读
        $no = Db::table('up_notice')
            ->where('n_type',1)
            ->where('n_read','notlike',"%,$user_id,%")
            ->field('n_id,n_read')
            ->select();
        if($no){
            foreach ($no as $k=>$v){
                $str = $v['n_read'].$user_id.',';
                Db::table('up_notice')
                    ->where('n_id',$v['n_id'])
                    ->update(['n_read'=>$str]);
            }
        }
        if($data){
            foreach ($data as $k2=>$v2){
                if(strpos($v2['n_read'],",$user_id,") !== false){//已读，不显示红点
                    $data[$k2]['read'] = 1;
                }else{
                    $data[$k2]['read'] = 2;//未读，显示红点
                }
            }
        }
        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('title','消息');
        return $this->fetch('user/news_sys');
    }

    /**
     * 系统消息
     * @return mixed
     */
    public function sysnewsinfo(){
        extract(input());
        $user_id = session('user_id');
        $data = Db::table('up_notice')
            ->where('n_id',$n_id)
            ->find();
        if(strpos($data['n_read'],",$user_id,") !== false){//包含就算了
            $data['nothing'] = 1;
        }else{
            $str = $data['n_read'].$user_id.',';
            Db::table('up_notice')
                ->where('n_id',$data['n_id'])
                ->update(['n_read'=>$str]);
        }
        $this->assign('d',$data);
        $this->assign('title','消息');
        return $this->fetch('user/news_sysinfo');
    }
}